package com.knight.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knight.mall.enums.BizSupportCode;
import com.knight.mall.exception.ServiceException;
import com.knight.mall.mappers.EnterpriseLogisticsMapper;
import com.knight.mall.model.EnterpriseLogistics;
import com.knight.mall.model.LogisticsCompany;
import com.knight.mall.query.EnterpriseLogisticsQuery;
import com.knight.mall.service.EnterpriseLogisticsService;
import com.knight.mall.service.LogisticsCompanyService;

/**
 * 商家-物流公司服务类实现
 * 
 * @author <a href="mailto:xbd0723@163.com">LiXuan</a> 2016年9月22日下午5:12:25
 */
@Service("enterpriseLogisticsService")
public class EnterpriseLogisticsServiceImpl implements EnterpriseLogisticsService {

	private static final Logger logger = LoggerFactory.getLogger(EnterpriseLogisticsServiceImpl.class);

	@Autowired
	EnterpriseLogisticsMapper enterpriseLogisticsMapper;
	@Autowired
	LogisticsCompanyService logisticsCompanyService;

	@Override
	public Long add(EnterpriseLogistics enterpriseLogistics) throws ServiceException {
		if (enterpriseLogistics.getEnterpriseId() == null) {
			logger.warn(BizSupportCode._2001002.getMsg());
			throw new ServiceException(BizSupportCode._2001002.getCode(), BizSupportCode._2001002.getMsg());
		}
		if (enterpriseLogistics.getLogisticsId() == null) {
			logger.warn(BizSupportCode._6000001.getMsg());
			throw new ServiceException(BizSupportCode._6000001.getCode(), BizSupportCode._6000001.getMsg());
		}
		LogisticsCompany logisticsCompany = logisticsCompanyService.load(enterpriseLogistics.getLogisticsId());
		if (logisticsCompany == null) {
			logger.warn(BizSupportCode._6000002.getMsg());
			throw new ServiceException(BizSupportCode._6000002.getCode(), BizSupportCode._6000002.getMsg());
		}
		enterpriseLogistics.setLogisticsCode(logisticsCompany.getCode());
		enterpriseLogistics.setLogisticsName(logisticsCompany.getName());
		enterpriseLogistics.setGmtCreate(new Date());
		enterpriseLogistics.setGmtModify(new Date());
		enterpriseLogistics.setStatus(1);
		enterpriseLogisticsMapper.insert(enterpriseLogistics);
		return enterpriseLogistics.getId();

	}

	@Override
	public void update(EnterpriseLogistics enterpriseLogistics) {
		enterpriseLogisticsMapper.update(enterpriseLogistics);
	}

	@Override
	public List<EnterpriseLogistics> queryList(EnterpriseLogisticsQuery query) {
		return enterpriseLogisticsMapper.queryList(query);
	}

	@Override
	public List<EnterpriseLogistics> queryListByEnterpriseId(Long id) {

		EnterpriseLogisticsQuery query = new EnterpriseLogisticsQuery();
		query.setEnterpriseId(id);
		query.setStatus(1);
		List<EnterpriseLogistics> list = enterpriseLogisticsMapper.queryList(query);

		return list;
	}

	@Override
	public int queryCount(EnterpriseLogisticsQuery query) {
		return enterpriseLogisticsMapper.queryCount(query);
	}

	@Override
	public EnterpriseLogistics load(Long id) {
		EnterpriseLogistics enterpriseLogistics = enterpriseLogisticsMapper.load(id);
		return enterpriseLogistics;
	}

	@Override
	public void delete(Long id) {
		 enterpriseLogisticsMapper.delete(id);
	}
}
